package com.java8feature.java8action.streamTest;

import com.test.stream.User;
import org.apache.poi.ss.formula.functions.T;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/6/10 14:00
 */
public class StreamTest {
    public static void main(String[] args) {
        ArrayList<User> arrayList = new ArrayList();
        User user1 = new User();
        user1.setName("user1");
        user1.setId(1);
        user1.setStatus(1);
        user1.setType(2);

        User user4 = new User();
        user4.setName("user4");
        user4.setId(1);
        user4.setStatus(2);
        user4.setType(1);

        User user2 = new User();
        user2.setName("user2");
        user2.setId(1);
        user2.setStatus(2);
        user2.setType(3);


        User user5 = new User();
        user5.setName("user5");
        user5.setId(1);
        user5.setStatus(3);
        user5.setType(3);


        User user3 = new User();
        user3.setName("user3");
        user3.setId(3);
        user3.setStatus(3);
        user3.setType(3);

        arrayList.add(user4);
        arrayList.add(user1);
        arrayList.add(user2);
        arrayList.add(user3);
        arrayList.add(user5);

        arrayList.parallelStream().forEach(u -> {
            System.out.println(u.getId());
            System.out.println( Thread.currentThread());});
        arrayList.sort(
                Comparator.comparing(User::getId).reversed().
                        thenComparing(Comparator.comparing(User::getType)
                                .thenComparing(Comparator.comparing(User::getStatus).reversed())
                               ).thenComparing(Comparator.comparing(User::getName)));
        System.out.println(arrayList);

        System.out.println(arrayList.stream().max(Comparator.comparing(User::getId)));

        Map<Integer, List<User>>  res = arrayList.stream().collect(Collectors.groupingBy(User::getType,LinkedHashMap::new

        ,Collectors.toList()));

//        HashMap<Integer, Map<Integer, String>> hashMap = arrayList.stream().collect(
//                Collectors.groupingBy(User::getType,
//                        //,HashMap::new,
//                        HashMap::new,
//                        Collectors.toMap(u1->{return u1.getId();},u2 -> {return u2.getName();})));

        arrayList.stream().collect(Collectors.partitioningBy(user -> user.getId() == 1));
        User indentity = new User();
        indentity.setId(100);
        User user = arrayList.stream().reduce(indentity,
                (i,j) ->{
                    i.setId(i.getId() + j.getId());
                    return i;
                });
        System.out.println("==================reduce ID=================");
        arrayList.stream().map(User::getName).collect(Collectors.joining());
        arrayList.stream().collect(Collectors.reducing(new User(),
                (s1,s2) ->{
                    s1.setName(s1.getName() + s2.getName()) ;
                    return s1;
                }
                ));
        System.out.println(user.getId());
        System.out.println(res);
        // arrayList.stream().allMatch()
//
//        arrayList.stream().filter(user -> user.getId() == 2).distinct().limit(2).count();
//
//        Stream<User> stream = arrayList.stream();
//        stream.forEach(
//                u ->{
//                    System.out.println(u.getId());
//                }
//        );
//        stream.forEach(user -> System.out.println(user.getId()));
        // Comparator可以实现排序
    }
}
